package day12;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.Scanner;



public class process {
	//static String userdir = System.getProperty("");
	//static String cmd = userdir+"/src/day12/test.bat";
	public void fun1(){
		
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("cmd /c dir");
			InputStream  stdin = proc.getInputStream();
			InputStreamReader  isr = new InputStreamReader(stdin);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while((line = br.readLine())!=null){
				System.out.println(line);
			}
			System.out.println("");
			int exitVal=proc.waitFor();
			System.out.println("Process exitValue");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void fun2(){
		String[] env = {"JAVA_HOME=F:/Java/jdk1.7.0_51",
				"PATH=F:/Java/jdk1.7.0_51/bin"};
		File dir = new File("E:/");
		try {
			Runtime rt = Runtime.getRuntime();//
			Process proc = rt.exec(new String[] {"cmd", "/c", "java", "-version"},env,dir);
			Scanner s1= new Scanner(proc.getInputStream());
			Scanner s2= new Scanner(proc.getErrorStream());
			StringBuilder r = new StringBuilder();
			while(s1.hasNextLine()){
				r.append(s1.nextLine());
				r.append(System.getProperty("line.separator"));
			}
			while(s2.hasNextLine()){
				r.append(s2.nextLine());
				r.append(System.getProperty("line.separator"));
			}
			s1.close();
			s2.close();
			System.out.print(r.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void fun3(){
		ProcessBuilder pr = new ProcessBuilder("cmd", "/c", "java", "-version");
		pr.redirectErrorStream(true);
		pr.directory(new File("E:/"));
		pr.environment().put("Path", "F:/Java/jdk1.7.0_51/bin");
		pr.environment().put("JAVA_HOME", "F:/Java/jdk1.7.0_51");
		try {
			Process ps = pr.start();
			Scanner s= new Scanner(ps.getInputStream());
			StringBuilder r = new StringBuilder();
			while(s.hasNextLine()){
				r.append(s.nextLine());
				r.append(System.getProperty("line.separator"));
			}
			s.close();
			System.out.print(r.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
